
<!-- 人脸识别统计 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item :label="v.name" :prop="v.prop" v-for="v in searchItems"
        :style="{ width: v.type == 'dateTime' ? '448px' : '' }">
        <el-input v-if="v.type == 'input'" v-model="queryParams[v.prop]" :placeholder="'请输入' + v.name" clearable
          @keyup.enter="handleQuery" />
        <el-tree-select v-else-if="v.type == 'treeSelect'" v-model="queryParams[v.prop]" :placeholder="'请选择' + v.name"
          clearable :data="sel_option[v.prop]" :expand-on-click-node="false" :render-after-expand="false"
          @change="handleQuery" check-strictly :props="{ label: 'label' }" node-key="id" filterable />
        <el-date-picker v-else-if="v.type == 'date'" v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" :default-time="[new Date(2000, 1, 1, 0, 0, 0),
        new Date(2000, 2, 1, 23, 59, 59),]" type="daterange" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
        <el-select v-else-if="v.type == 'select' && v.prop == 'result'" v-model="queryParams[v.prop]" clearable filterable
          :placeholder="'请输入' + v.name" @change="handleQuery">
          <el-option v-for="item in sel_option.successFail" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['statisticalReportForm:faceRecognitionStatistics:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table highlight-current-row v-loading="loading" :data="tableList">
      <template v-for="v in tableItems">
        <el-table-column :label="v.name" align="left" :prop="v.prop">
          <template #default="scope">
            <span v-if="v.prop == 'picture'">
              <el-image style="width: 100px; height: 50px" :preview-teleported="true" :src="getImgSrc(scope.row.picture)"
                :preview-src-list="[getImgSrc(scope.row.picture)]">
              </el-image>
            </span>
            <span v-else> {{ scope.row[v.prop] }}</span>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>
<script setup lang="ts" name="FaceRecognitionStatistics">
import dayjs from "dayjs"
import { reactive, toRefs, ref, getCurrentInstance } from "vue"
import { treeDept } from "../../api/system/dept";
import request from '../../utils/request'
const loading = ref(false);
const tableList = ref([]);
const { proxy } = getCurrentInstance();
const tableItems = ref([
  { name: '设备名称', prop: "numberPlate" },
  { name: '所属公司', prop: "companyName" },
  { name: '姓名', prop: "driverName" },
  { name: '识别结果', prop: "resultStr" },
  { name: '识别时间', prop: "time" },
  { name: '识别图片', prop: "picture" },
]);
const searchItems = ref([
  { name: '设备名称', prop: "numberPlate", type: 'input' },
  { name: '所属公司', prop: "companyId", type: 'treeSelect' },
  { name: '识别结果', prop: "result", type: 'select' },
  { name: '统计日期', prop: "time", type: 'date' },
]);
const total = ref(0);
const showSearch = ref(true);
const dateRange: any = ref([
  dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD 00:00:00"),
  dayjs(new Date()).format("YYYY-MM-DD 23:59:59")
]);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    companyId: "",
    numberPlate: "",
    result: "",
  },
});
const sel_option = ref({
  companyId: null,
  successFail: []
});
//@ts-ignore
sel_option.value = proxy.useDict.apply(this, ["successFail"]);

treeDept().then(res => {
  sel_option.value.companyId = res.data
})
const { queryParams } = toRefs(data);

function getImgSrc(val: string) {
  if (val) {
    //@ts-ignore
    return import.meta.env.VITE_APP_BASE_API + val;
  }
  else return "";
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  //@ts-ignore
  proxy.resetForm("queryRef");
  dateRange.value = [
    dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD 00:00:00"),
    dayjs(new Date()).format("YYYY-MM-DD 23:59:59")
  ];
  handleQuery();
}
/** 查询列表 */
function getList() {
  loading.value = true;
  let data = {
    pageSize: queryParams.value.pageSize,
    pageIndex: queryParams.value.pageNum,
    startTime: dateRange.value ? dateRange.value[0] : '',
    endTime: dateRange.value ? dateRange.value[1] : '',
    companyId: queryParams.value.companyId,
    numberPlate: queryParams.value.numberPlate,
    result: queryParams.value.result
  }
  request({
    url: '/comlbs/reportStatistics/personFaceResult/list',
    method: 'post',
    data: data
  }).then(({ data }: any) => {
    tableList.value = data.list;
    total.value = data.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  })
}

/** 导出按钮操作 */
function handleExport() {
  let data = {
    startTime: dateRange.value ? dateRange.value[0] : '',
    endTime: dateRange.value ? dateRange.value[1] : '',
    companyId: queryParams.value.companyId,
    numberPlate: queryParams.value.numberPlate,
    result: queryParams.value.result
  }
  //@ts-ignore
  proxy.download('comlbs/reportStatistics/personFaceResult/listExport',
    data
    , `人脸识别统计_${dayjs(new Date()).format("YYYY-MM-DD_HH:mm:ss")}.xlsx`)
}
getList();
</script>
<style lang='scss' scoped></style>